import { useRequest } from "ahooks";
import { useSearchParams } from "react-router-dom";
import {
  LIST_PAGE_PARAM_KEY,
  LIST_PAGE_SIZE,
  LIST_SEARCH_PARAM_KEY,
  LIST_PAGE_SIZE_PARAM_KEY,
} from "../constant";
import { getQuestionListService } from "../service/question";

type OptionType = {
  isStar: boolean;
  isDeleted: boolean;
};
function useLoadQuestionListData(opt: Partial<OptionType> = {}) {
  opt = opt || {};
  let params: any = {};
  const [searchParams] = useSearchParams();

  const keyword = searchParams.get(LIST_SEARCH_PARAM_KEY) || "";
  const page = parseInt(searchParams.get(LIST_PAGE_PARAM_KEY) || "1");
  const pageSize = parseInt(
    searchParams.get(LIST_PAGE_SIZE_PARAM_KEY) || LIST_PAGE_SIZE
  );
  params = { ...opt, keyword, page, pageSize };
  const { data, loading, error, refresh } = useRequest(
    async () => {
      const data = await getQuestionListService(params);
      return data;
    },
    {
      refreshDeps: [searchParams],
      debounceWait: 400,
    }
  );
  return { data, loading, error, refresh };
}
export default useLoadQuestionListData;
